<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("customerSession.inc.php");
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");
include_once("UploadFiles.php");				//--> UploadFiles
include_once("Customer.php");					//--> Customer
include_once("Validation.php");					//--> Validation
include_once ("Session.php");					//-- Session

include_once("Strings.php");					//--> String utils

include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart

require_once('includeHttps.php');

$common 		= new Common();
$objCustomer	= new Customer(DB_TAG_SYSTEM, $uid);	//--> Customer
$objValidation	= new Validation($uid);					//--> Validation
$objSession		= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session

$objCart 		= new Cart();		//--> Cart
$objStrings 	= new Strings();

$customerid 	= 0;
$isLogin		= false;

/*----- check session -----*/
if($objSession->exist()) {
	$isLogin	= true;
	
	$customerid = $objSession->getUserID();
}else{
	$location = './myaccount.php';
	
	header("Location: $location");
	exit;
}

if($_SESSION['cart'])
	$objCart = unserialize($_SESSION['cart']);

$firstname 		= '';
$lastname 		= '';
$email 			= '';
$emailOld		= '';

$telephone		= '';
$fax			= '';


if(!empty($customerid)){
	$arrCustomer = $common->getRow(DB_TAG_PUBLIC, "SELECT * FROM customers WHERE id = $customerid");
	
	$firstname 		= $arrCustomer['firstname'];
	$lastname 		= $arrCustomer['lastname'];
	$emailOld		= $arrCustomer['email'];
	$email			= $arrCustomer['email'];
	
	$telephone		= $arrCustomer['telephone'];
	$fax			= $arrCustomer['fax'];
}


if($_POST['email']){
	$firstname 		= $_POST['firstname'];
	$lastname 		= $_POST['lastname'];
	$email 			= $_POST['email'];
	
	$telephone		= $_POST['telephone'];
	$fax			= $_POST['fax'];
		
	
	if(empty($firstname))
		$error_message = 'The first name should\'t be empty.';
	else if(empty($lastname)){
		$error_message = 'The first name should\'t be empty.';
	}else if(empty($email)){
		$error_message = 'The email should\'t be empty.';
	}else if(!$objValidation->isEmail($email)){
		$error_message = 'The email format invalid.';
	}else if($objCustomer->IsExistEmail($email) && $emailOld != $email){
		$error_message = 'Your E-Mail Address already exists in our records - please log in with the e-mail address or create an account with a different address.';
	}
	
	if(empty($error_message)){
		$sql = "UPDATE customers SET modifiedTime = UTC_TIMESTAMP(), firstname = '$firstname', lastname = '$lastname', email = '$email', " .
				"telephone = '$telephone', fax='$fax' WHERE id = $customerid";
		$isSuccess = $common->update(DB_TAG_SYSTEM, $sql);
		if($isSuccess){
			$error_message = 'Changed successfully.';
		}else{
			$error_message = 'Changed failure.';
		}
	}
}


include_once("includeCategory.php");	//--> include category
include_once("includeSpec.php");		//--> include spec


/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->force_compile 	= true;
$smarty->debugging 		= false;
$smarty->caching 		= false;
$smarty->cache_lifetime = 120;

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('error_message', $error_message);

$smarty->assign('categorys', $categorys);
$smarty->assign('topCategory', $topCategory);
$smarty->assign('specProducts', $specProducts);

$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

$smarty->assign('firstname', $firstname);
$smarty->assign('lastname', $lastname);
$smarty->assign('email', $email);

$smarty->assign('telephone', $telephone);
$smarty->assign('fax', $fax);

$smarty->assign('isLogin', $isLogin);

$smarty->display('accountEdit.html');
?>
